<template>
  <div class="dashboard-customization">
    <!-- 顶部区域 -->
    <div class="top-section">
      <div class="header">
        <label for="report-name">报告名称</label>
        <input
            id="report-name"
            v-model="reportName"
            link
            placeholder="请输入自定义报告名称"
        />
      </div>
      <div class="footer">
        <button @click="submitSelection">完成</button>
      </div>
    </div>

    <!-- 布局容器 -->
    <div class="layout-container">
      <!-- 模板选择区域 -->
      <div class="template-selection">
        <div class="template-header">
          <label class="template-label" for="template-search">
            选择已有模板
            <span v-if="selectedTemplate" class="selected-template-name">
              ({{ selectedTemplate.name }})
            </span>
          </label>
        </div>
        <div class="template-list">
          <input
              id="template-search"
              v-model="templateSearch"
              class="template-search-input"
              link
              placeholder="按名称搜索模板"
          />
          <ul class="template-ul">
            <li
                v-for="template in filteredTemplates"
                :key="template.id"
                :class="{ selected: selectedTemplate === template }"
                class="template-item"
                @click="selectTemplate(template)"
                @dblclick="deselectTemplate(template)"
                @mouseleave="hidePreview"
                @mouseover="showPreview(template)"
            >
              <span class="template-name">{{ template.name }}</span>
            </li>
          </ul>
        </div>
      </div>

      <!-- 自定义表盘 -->
      <div class="custom-dashboard">
        <div class="template-header">
          <label class="template-label" for="template-search">
            或自定义表盘
          </label>
        </div>
        <div class="card-grid">
          <div
              v-for="(card, index) in cardList"
              :key="index"
              :class="{ selected: selectedCards.includes(index) }"
              class="card"
              @click="toggleCardSelection(index)"
          >
            <div class="icon">{{ card.icon }}</div>
            <div class="title">{{ card.title }}</div>
            <div v-if="selectedCards.includes(index)" class="corner-indicator"></div>
          </div>
        </div>
      </div>
    </div>

    <!-- 预览浮窗 -->
    <div
        v-if="showingPreview"
        :style="{ top: previewPosition.top, left: previewPosition.left }"
        class="template-preview"
    >
      <div class="preview-header">{{ previewTemplate.name }}</div>
      <div class="preview-content">
        <p>示例表盘样式预览...</p>
        <!-- 示例表盘样式 -->
        <div class="sample-dashboard">
          <div class="sample-card">图表1</div>
          <div class="sample-card">图表2</div>
          <div class="sample-card">图表3</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script name="DashboardCreate">
export default {
  data() {
    return {
      templates: Array.from({length: 80}, (_, i) => ({
        id: i + 1,
        name: `模板${i + 1}`,
      })),
      templateSearch: "",
      selectedTemplate: null,
      reportName: "",
      selectedCards: [],
      cardList: [
        {title: "卡片1", icon: "📊"},
        {title: "卡片2", icon: "📊"},
        {title: "卡片3", icon: "📊"},
        {title: "卡片4", icon: "📊"},
        {title: "卡片5", icon: "📊"},
        {title: "卡片6", icon: "📊"},
        {title: "卡片7", icon: "📊"},
        {title: "卡片8", icon: "📊"},
        {title: "卡片9", icon: "📊"},
        {title: "卡片10", icon: "📊"},
        {title: "卡片11", icon: "📊"},
        {title: "卡片12", icon: "📊"},
        {title: "卡片13", icon: "📊"},
        {title: "卡片14", icon: "📊"},
        {title: "卡片15", icon: "📊"},
        {title: "卡片16", icon: "📊"},
        {title: "卡片17", icon: "📊"},
        {title: "卡片18", icon: "📊"},
        {title: "卡片19", icon: "📊"},
        {title: "卡片20", icon: "📊"},
        {title: "卡片21", icon: "📊"},
        {title: "卡片22", icon: "📊"},
        {title: "卡片23", icon: "📊"},
        {title: "卡片24", icon: "📊"},
        {title: "卡片25", icon: "📊"},
        {title: "卡片26", icon: "📊"},
        {title: "卡片27", icon: "📊"},
        {title: "卡片28", icon: "📊"},
        {title: "卡片29", icon: "📊"},
        {title: "卡片30", icon: "📊"},
        {title: "卡片31", icon: "📊"},
        {title: "卡片32", icon: "📊"},
        {title: "卡片33", icon: "📊"},
        {title: "卡片34", icon: "📊"},
        {title: "卡片35", icon: "📊"},
      ],
      showingPreview: false,
      previewTemplate: null,
      previewPosition: {top: "0px", left: "0px"},
    };
  },
  computed: {
    filteredTemplates() {
      return this.templates.filter((template) =>
          template.name.includes(this.templateSearch)
      );
    },
  },
  methods: {
    selectTemplate(template) {
      this.selectedTemplate = template;
      this.selectedCards = [];
    },
    deselectTemplate(template) {
      if (this.selectedTemplate === template) {
        this.selectedTemplate = null;
      }
    },
    toggleCardSelection(cardId) {
      if (this.selectedTemplate) return;
      const index = this.selectedCards.indexOf(cardId);
      if (index > -1) {
        this.selectedCards.splice(index, 1);
      } else {
        this.selectedCards.push(cardId);
      }
    },
    submitSelection() {
      if (this.selectedTemplate) {
        alert(`已选择模板：${this.selectedTemplate.name}`);
      } else if (this.selectedCards.length > 0) {
        alert(`已选择卡片：${this.selectedCards.join(", ")}`);
      } else {
        alert("未选择任何内容！");
      }
    },
    showPreview(template) {
      this.showingPreview = true;
      this.previewTemplate = template;
      this.previewPosition = {top: event.clientY + "px", left: event.clientX + "px"};
    },
    hidePreview() {
      this.showingPreview = false;
      this.previewTemplate = null;
    },
  },
};
</script>

<style lang="scss" scoped>
.dashboard-customization {
  padding: 20px;
  background: #ffffff;
  height: 740px;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
}

/* 顶部区域 */
.top-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.header {
  display: flex;
  align-items: center;
}

.header label {
  margin-right: 10px;
  font-weight: bold;
}

.header input {
  width: 300px;
  height: 40px;
  padding: 8px;
  border: 1px solid #ccc;
  border-radius: 4px;
  font-size: 16px;
}

.footer button {
  padding: 10px 20px;
  background: #1890ff;
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  transition: background 0.3s;
}

.footer button:hover {
  background: #40a9ff;
}

/* 布局容器 */
.layout-container {
  display: flex;
  gap: 20px;
  flex: 1;
  overflow: hidden;
  margin: 0 20px;
}

/* 模板选择样式 */
.template-selection,
.custom-dashboard {
  flex: 1;
  display: flex;
  flex-direction: column;
  width: 35%; /* 宽度各占 35% */
}

.template-label {
  font-size: 18px;
  font-weight: bold;
  color: #333;
  margin-bottom: 10px;
}

.selected-template-name {
  color: #1890ff;
  font-size: 16px;
  margin-left: 10px;
}

.template-list {
  position: relative;
  margin-top: 15px;
  flex: 1;
  overflow-y: auto;
}

.template-search-input {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 14px;
  margin-bottom: 10px;
}

.template-ul {
  margin: 0;
  padding: 0;
  list-style: none;
  border: 1px solid #ddd;
  border-radius: 4px;
  background: #fff;
}

.template-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  font-size: 16px;
  color: #333;
  cursor: pointer;
}

.template-item.selected {
  background: #e6f7ff;
  color: #1890ff;
}

.template-name {
  flex-grow: 1;
}

/* 自定义表盘样式 */
.card-grid {
  display: grid;
  margin-top: 15px;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 16px;
  overflow-y: auto;
  flex: 1;
}

.card {
  width: 90px;
  height: 90px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  border: 1px solid #ddd;
  border-radius: 8px;
  background: #fff;
  position: relative;
  cursor: pointer;
}

.card.selected {
  background: #1890ff;
  color: #fff;
  border-color: #1890ff;
}

.card .icon {
  font-size: 24px;
}

.card .title {
  font-size: 14px;
  text-align: center;
}

.card .corner-indicator {
  position: absolute;
  top: 5px;
  right: 5px;
  width: 10px;
  height: 10px;
  background: blue;
  border-radius: 50%;
}

/* 预览浮窗样式 */
.template-preview {
  position: absolute;
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 4px;
  padding: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.preview-header {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 10px;
}

.preview-content {
  font-size: 14px;
}

.sample-dashboard {
  display: flex;
  gap: 10px;
  margin-top: 10px;
}

.sample-card {
  padding: 20px;
  background: #f5f5f5;
  border-radius: 4px;
  text-align: center;
  flex: 1;
}
</style>
